//
//  ViewController.m
//  09-UITableView编辑
//
//  Created by GivenBmli on 16/7/26.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#define  kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic ,strong)NSMutableArray *nameArray;
@property (nonatomic ,strong)UITableView *tableView;
/**选中下标的数组*/
@property (nonatomic , strong)NSMutableArray <NSIndexPath *>*selectIndexArray;
/**选中名字的数组*/
@property (nonatomic , strong)NSMutableArray <NSString *> *selectNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndexArray = [NSMutableArray array];
    self.selectNameArray  =[NSMutableArray array];
//    self.nameArray = [[NSMutableArray alloc]init];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.delegate =self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
//     tableView编辑
//    1.设置第一步barButtonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    重写rightBarButtonItem方法
    NSLog(@"%@",NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
    
}

- (void)editAction {
//    [self respondsToSelector:<#(SEL)#>]
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (!self.editing == NO) {
      
    
    }
}
#pragma make - tableView Delegate
// 4.返回cell的编辑状态(样式)默认Delete
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//    返回UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete 多选
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
//       取出对应下标的名字
//        添加到选中名字的数中
//       在全选状态下,数组中添加对应选中的下标
        [_selectNameArray addObject:_nameArray[indexPath.row]];
        [_selectIndexArray addObject:indexPath];
        
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
//        取出对应下标的名字
        NSString *name = _nameArray[indexPath.row];
        //        从选中名字的数组中移除
        [_selectNameArray removeObject:name];

        
//                在全选状态下,数组中移除对应选中的下标
        [_selectIndexArray removeObject:indexPath];
        
    }
    
}

#pragma make - tableView DataSource
//Eide
// 2.让tableView进编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    }else {
//       要删除选中的数组
//       删除数据源
        [_nameArray removeObjectsInArray:_selectNameArray];
//       删除cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        NSLog(@"完成");
    }
}
// 3.控制tableView 能否被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


// 5.对应按钮的点击方法
//滑动删除只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
//        对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        [_nameArray insertObject:@"王煌" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        NSLog(@"插入");
    }
    
//    不论是删除还是添加,都已经进行了刷新的操作
}
// 能否移动 默认可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 更改数据源 , 移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

// DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
